/*
 Copyright (c) 2008 TPSi
 Alex Iskander
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 */
#include <sstream>


TEST(BoundTest, IsWorking)
{
	/* Test setting */
	Bind::Bound<int> b1;
	
	//test that it initializes to 0
	ASSERT_EQ(b1.get(), 0);
	
	b1 = 5;
	
	ASSERT_EQ(b1.get(), 5);
	
	/* Test Connecting */
	Bind::Bound<int> b2;
	b2 = b1;
	
	EXPECT_EQ(b2.get(), 5);
	
	/* Test Updating */
	b1 = 3;
	
	EXPECT_EQ(b2.get(), 3);
	
	/* Test conversion */
	int result = b2;
	
	EXPECT_EQ(result, 3);
}

TEST(BoundTest, Overriding)
{
	/* Setup */
	Bind::Bound<int> b1;
	b1 = 5;
	
	Bind::Bound<int> b2;
	b2 = b1;
	
	// disconnect/override
	b2 = 3;
	
	//check
	EXPECT_EQ(b2.get(), 3);
	
	//double-check (highly unlikely to fail)
	EXPECT_EQ(b1.get(), 5);
	
	// update
	b1 = 3;
	
	//check
	EXPECT_EQ(b2.get(), 3);
}

TEST(BoundTest, Chaining)
{
	Bind::Bound<int> base;
	Bind::Bound<int> second;
	Bind::Bound<int> third;
	
	//set up
	second = base;
	third = second;
	
	//test
	base = 5;
	EXPECT_EQ(base.get(), 5);
	EXPECT_EQ(second.get(), 5);
	EXPECT_EQ(third.get(), 5);
	
	//and unlink second
	second = 3;
	
	EXPECT_EQ(second.get(), 3);
	EXPECT_EQ(third.get(), 3);
	
	//and make sure everything is disconnected
	base = 1;
	
	EXPECT_EQ(base.get(), 1);
	EXPECT_EQ(second.get(), 3);
	EXPECT_EQ(third.get(), 3);
	
}

TEST(BoundTest, EqualityOperator)
{
	Bind::Bound<int> x;
	x= 5;
	Bind::Bound<int> y;
	y = 10;
	
	EXPECT_TRUE((x == 5));
	EXPECT_TRUE((y == 10));
	
	EXPECT_FALSE((x == y));
	
	EXPECT_TRUE((x != y));
}

TEST(BoundTest, OStream)
{
	Bind::Bound<int> x;
	x = 5;
	std::ostringstream stream;
	stream << x;
	
	EXPECT_EQ(stream.str(), "5");
	
	stream << x << x << x;
	
	EXPECT_EQ(stream.str(), "5555");
}

TEST(BoundTest, Baking)
{
	Bind::Bound<int> x;
	Bind::Bound<int> y;
	
	x = y;
	y = 5;
	
	//just ensure things are working
	EXPECT_EQ(x, 5);
	
	//now, bake x
	x.bake();
	
	//set y to something else
	y = 10;
	
	//x should remain the same
	EXPECT_EQ(x, 5);
}

TEST(BoundTest, BiDirectional)
{
	Bind::Bound<int> x;
	Bind::Bound<int> y;
	
	x = 5;
	
	//establish a bidirectional bind... this tells x and y to NOT disconnect
	//from each other when their values change.
	y.biBind(x);
	
	EXPECT_EQ(x, 5);
	EXPECT_EQ(y, 5);
	
	y = 3;
	
	EXPECT_EQ(y, 3);
	EXPECT_EQ(x, 3);
	
	/* Now, unless bi-directional binding is truly supported, setting
	 y to 3 will have disconnected y from x. Let us try it... */
	x = 7;
	
	EXPECT_EQ(x, 7);
	EXPECT_EQ(y, 7);
	
	/* And just to be sure, set y again */
	y = 8;
	EXPECT_EQ(y, 8); 
	EXPECT_EQ(x, 8);
}

TEST(BoundTest, IsSet)
{
	Bind::Bound<int> x;
	
	EXPECT_FALSE(x.isSet()) << "X should not be set yet";
	
	x = 5;
	
	EXPECT_TRUE(x.isSet()) << "X should now be set";
	
	x.unset();
	
	EXPECT_FALSE(x.isSet()) << "X should no longer be set";
	
	Bind::Bound<int> y;
	
	EXPECT_FALSE(y.isSet()) << "Y should not be set yet";
	
	
	y = x;
	
	EXPECT_FALSE(y.isSet()) << "Y should still not be set";
	
	x = 5;
	
	EXPECT_TRUE(y.isSet()) << "Y should now be set.";
	
	x.unset();
	
	EXPECT_FALSE(y.isSet()) << "Y should now not be set.";
	
}

class TestDestructionListener : public Bind::DestructionListener
{
public:
	TestDestructionListener()
	{
		this->_wasDestroyed = false;
	}
	
	void destroyed(Bind::Bindable *property) 
	{
		this->_wasDestroyed = true;
	}
	
	bool wasDestroyed()
	{
		return this->_wasDestroyed;
	}
	

protected:
	bool _wasDestroyed;
};

TEST(BoundTest, DestructionListening)
{
	//create a pointer - so we can track destruction
	Bind::Bound<int> *x = new Bind::Bound<int>();
	
	TestDestructionListener *listener = new TestDestructionListener();
	
	EXPECT_FALSE(listener->wasDestroyed());
	
	x->onDestroy(listener);
	
	EXPECT_FALSE(listener->wasDestroyed());
	
	delete x;
	
	EXPECT_TRUE(listener->wasDestroyed());
	
	delete listener;
}


/**
 \class CustomGetter;
 \brief Tests custom-made getters, and the passing of This to them.
 */
class CustomGetter : public Bind::GetterFunction<std::string>
{
public:
	std::string operator() (const Bind::Bindable *This);
};

/**
 \class TestClass;
 This test class has a bound value "testValue", and a getter and setter for it.
 The return value is always equal to seven times whatever the actual value is.
 */


class TestClass : public Bind::BindableObject
{
public:
	TestClass()
	{
		this->testValue.getter = Bind::getter(this, &TestClass::getTestValue);
		this->testValue.setter = Bind::setter(this, &TestClass::setTestValue);
		
		this->addProperty("test", this->stringValue);
		this->stringValue.getter = new CustomGetter();
		this->stringValue.setter = Bind::setter(this, &TestClass::setStringValue);
	}
	


	Bind::Bound<int> testValue;
	Bind::Bound<std::string> stringValue;
	int getTestValue() const {
		return xVal * 7;
	}
	
	std::string getStringValue()
	{
		return this->sVal;
	}
protected:

	
	void setTestValue(int value)
	{
		xVal = value;
	}
	
	void setStringValue(std::string value)
	{
		this->sVal = value;
	}
	
	std::string getStringValue() const
	{
		return this->sVal;
	}
	
	int xVal;
	std::string sVal;
};

std::string CustomGetter::operator() (const Bind::Bindable *This)
{
	Bind::BindableObject *parent = This->getParent();
	if (parent)
	{
		std::string s = static_cast<TestClass *> (parent)->getStringValue();
		
		s += " Test!";
		
		return s;
	}
	else return "";
}


TEST(BoundTest, GettersSetters)
{
	TestClass test;
	test.testValue = 3;
	
	EXPECT_EQ(test.testValue, 3 * 7);
	
	Bind::Bound<int> test1;
	test1 = test.testValue;
	
	int val = test1.get();
	
	EXPECT_EQ(val, 3 * 7);
	
	test.testValue = 7;
	
	EXPECT_EQ(test1, 7 * 7);
	
	test.stringValue = "Hi";
	EXPECT_EQ(test.stringValue.get(), "Hi Test!");
	
	
}